package com.ktwlsoft.archivemanage.controller.businessInspectTem;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ktwlsoft.archivemanage.domain.business.businessService.businessInspectTem.BusinessInspectTemService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.OperationRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.OperationLog;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.businessInspectTem.BusinessInspectTemSaveRequest;
import com.ktwlsoft.archivemanage.payload.businessInspectTem.BusinessInspectTemUpdateRequest;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveLogRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 业务检查模板
 *
 * @author zhuhe on 2019/11/6 16:18
 * @version 1.0
 */
@Api(value = "业务检查模板", description = "业务检查模板")
@RestController
@RequestMapping("businessInspectTem")
public class BusinessInspectTemController {

    private final BusinessInspectTemService temService;

    public BusinessInspectTemController(BusinessInspectTemService temService) {
        this.temService = temService;
    }

    /**
     * 业务检查模板新增 on 2019/11/6 16:55
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "业务检查模板新增", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("save")
    @UserAuthenticate(actionCode = "BUSINESS_TEM_ADD")
    public ResponseEntity<?> save(@Valid @RequestBody BusinessInspectTemSaveRequest request) {
        Result<?> result = temService.save(request);
        if(result.getResultType().equals(ResultType.BUSINESS_SUCCESS)){
            saveLog(OperationLog.OperationType.ADD,"添加模板《" + request.getName() + "》");
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 业务检查模板修改 on 2019/11/7 9:10
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "业务检查模板修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("modify")
    @UserAuthenticate(actionCode = "BUSINESS_TEM_EDIT")
    public ResponseEntity<?> modify(@Valid @RequestBody BusinessInspectTemUpdateRequest request) {
        Result<?> result = temService.modify(request);
        if(result.getResultType().equals(ResultType.BUSINESS_SUCCESS)){
            saveLog(OperationLog.OperationType.EDIT,"编辑模板《" + request.getName() + "》");
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 业务检查模板删除 on 2019/11/7 10:27
     *
     * @param id id
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "业务检查模板删除", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("deleteById")
    @UserAuthenticate(actionCode = "BUSINESS_TEM_DELETE")
    public ResponseEntity<?> deleteById(@ApiParam(name = "id", value = "模板主键") @RequestParam(defaultValue = "") Long id){
        Result<?> result = temService.deleteById(id);
        if(result.getResultType().equals(ResultType.BUSINESS_SUCCESS)){
            saveLog(OperationLog.OperationType.DELETE,"删除模板《" + result.getData() + "》");
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 业务检查模板启用禁用 on 2019/11/7 10:27
     *
     * @param id id
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "业务检查模板启用禁用", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("enable")
    @UserAuthenticate(actionCode = "BUSINESS_TEM_ENABLE")
    public ResponseEntity<?> enable(@ApiParam(name = "id", value = "模板主键") @RequestParam(defaultValue = "") Long id){
        return ResponseEntity.ok(temService.enable(id));
    }

    /**
     * 模板复制 on 2019/11/7 10:27
     *
     * @param id id
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "模板复制", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("copy")
    @UserAuthenticate(actionCode = "BUSINESS_TEM_COPY", skip = true)
    public ResponseEntity<?> copy(@ApiParam(name = "id", value = "模板主键") @RequestParam(defaultValue = "") Long id){
        Result<?> result = temService.getByid(id);
        if(result.getResultType().equals(ResultType.BUSINESS_SUCCESS)){
            JSONObject object = JSONObject.parseObject(JSON.toJSONString(result.getData()));
            saveLog(OperationLog.OperationType.COPY,"复制模板《" + object.get("name") + "》");
        }
        return ResponseEntity.ok(temService.getByid(id));
    }

    /**
     * 获取模板详情 on 2019/11/7 10:27
     *
     * @param id id
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "获取模板详情", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("getByid")
    @UserAuthenticate(actionCode = "BUSINESS_TEM_DETAIL", skip = true)
    public ResponseEntity<?> getByid(@ApiParam(name = "id", value = "模板主键") @RequestParam(defaultValue = "") Long id){
        return ResponseEntity.ok(temService.getByid(id));
    }

    /**
     * 业务检查模板列表 on 2019/11/7 12:06
     *
     * @param operationStartTime operationStartTime
     * @param operationEndTime operationEndTime
     * @param enable enable
     * @param keyWords keyWords
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "业务检查模板列表", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("findPage")
    @UserAuthenticate(actionCode = "BUSINESS_TEM_LIST", skip = true)
    public ResponseEntity<?> findPage(@RequestParam(defaultValue = "1") int pn,
                                      @RequestParam(defaultValue = "30") int pageSize,
                                      @ApiParam(name = "operationStartTime", value = "操作开始时间") @RequestParam(defaultValue = "", required = false) String operationStartTime,
                                      @ApiParam(name = "operationEndTime", value = "操作结束时间") @RequestParam(defaultValue = "", required = false) String operationEndTime,
                                      @ApiParam(name = "enable", value = "状态:1.启用,0.禁用") @RequestParam(defaultValue = "", required = false) Integer enable,
                                      @ApiParam(name = "keyWords", value = "关键字") @RequestParam(defaultValue = "", required = false) String keyWords,
                                      @ApiParam(name = "inspectType", value = "检查类型:default(默认),self_or_repeat(自查复查)") @RequestParam(defaultValue = "", required = false) String inspectType,
                                      @ApiParam(name = "field", value = "排序字段") @RequestParam(defaultValue = "", required = false) String field,
                                      @ApiParam(name = "order", value = "排序方式:descend(降序),ascend(升序)") @RequestParam(defaultValue = "", required = false)String order){
        return ResponseEntity.ok(temService.findPage(pn,pageSize,operationStartTime,operationEndTime,enable,keyWords,inspectType,field,order));
    }

    /**
     * 新增日志 on 2019/11/14 10:37
     *
     * @param operationType 操作类型
     * @param description 描述
     */
    private void saveLog(OperationLog.OperationType operationType, String description){
        OperationRepositoryService.saveLog(new SaveLogRequest(
                operationType,
                description,
                "业务检查模板",
                OperationLog.BusinessType.BUSINESS
        ));
    }
}